package com.zpl.goods.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.zpl.entity.BaseEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangyuxin
 * @date 2020/9/28
 */
public enum  Status implements BaseEnum {
    RU(0,"入库"),
    UP(1,"上架"),
    DOWN(2,"下架");


    @EnumValue
    private Integer code;
    private String displayName;

    Status(Integer code, String displayName) {
        this.code = code;
        this.displayName = displayName;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    @Override
    public String getName() {
        return this.name();
    }

    @JsonValue
    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", this.getCode());
        map.put("displayName", this.getDisplayName());
        return map;
    }

    public static JSONArray toJson() {
        JSONArray jsonArray = new JSONArray();
        for (Status e : Status.values()) {
            JSONObject object = new JSONObject();
            object.put("code", e.getCode());
            object.put("displayName", e.getDisplayName());
            jsonArray.add(object);
        }
        return jsonArray;
    }
    @JsonCreator
    public static String getDisplayNameByCode(String code) {
        String displayName="";
        for (Status ele : values()) {
            if (ele.getCode().equals(Integer.valueOf(code))) {
                displayName=ele.getDisplayName();
                break;
            }
        }
        return displayName;
    }
}
